/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain.operativa;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;


import co.gov.medellin.sdep.sisdep.domain.MessageModalWindowController;
import co.gov.medellin.sdep.sisdep.domain.BaseController;
import co.gov.medellin.sdep.sisdep.operativa.exception.CharlaPAgendaException;
import co.gov.medellin.sdep.sisdep.operativa.exception.CharlaPAgendaRuntimeException;
import co.gov.medellin.sdep.sisdep.operativa.service.ICharlaPAgendaService;
import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.model.CharlaPAgenda;

/**
 * @author Usuario
 * 
 */
public class CharlaPAgendaController extends BaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;

	/** bean para el manejo del formulario de busqueda CharlaPAgenda */
	public CharlaPAgenda charlaPAgendaSearch = new CharlaPAgenda();

	/** Objeto contenedor del listado de CharlaPAgenda luego de una consulta. */
	private transient DataModel charlaPAgendaListModel;

	/** bean para el manejo del formulario de creacion/edicion. CharlaPAgenda */
	private CharlaPAgenda charlaPAgenda = new CharlaPAgenda();

	/** instancia de servicio charlaPAgendaService */
	private ICharlaPAgendaService charlaPAgendaService;

	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public CharlaPAgendaController() {
		this.charlaPAgendaListModel = new ListDataModel();
	}
	// --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------

	/**
	 * 
	 * @return String
	 */
	public String searchCharlaPAgendaByKey() {
		String msg = "";
		try {
			List valores = null;
			valores = getCharlaPAgendaService().load(1, 10,
					this.charlaPAgendaSearch);
			this.charlaPAgendaListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.charlaPAgendaListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.charlaPAgendaListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.CHARLA_P_AGENDA_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return String
	 */
	public String searchCharlaPAgendaByIdCharla(Long idCharla,ICharlaPAgendaService service) {
		String msg = "";
		try {
			List valores = null;
			valores = service.load(1, 10,
					this.charlaPAgendaSearch);
			this.charlaPAgendaListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.charlaPAgendaListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.charlaPAgendaListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.CHARLA_P_AGENDA_SEARCH_VIEW;
	}

  /**
	 * Metodo que permite espeficicar la funcionalidad para la gestion [Crear / Actualizar] de un elmento tipo:
	 * CharlaPAgenda.
	 *     
	 * @author programador.java@consult-soft.com  05-06-2012
	 * @return String Vista a la cual debe retornar
	 * 	 
	 */
	public String manage() {
		String returnView = AppConstants.CHARLA_P_AGENDA_SEARCH_VIEW;
		if(this.charlaPAgenda.getId() == null) {
       returnView = create();
		} else {
       returnView = update();
		}
		return returnView;
	}

	/**
	 * 
	 * @return
	 */
	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.charlaPAgenda.getId() == null) {
				getCharlaPAgendaService().insert(this.charlaPAgenda);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (CharlaPAgendaException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (CharlaPAgendaRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean(AppConstants.MESSAGE_CONTROLLER_BEAN))
				.setSuccessMessage("- charlaPAgenda " + msg, null, null);
		return AppConstants.CHARLA_P_AGENDA_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.charlaPAgenda.getId() != null) {
				getCharlaPAgendaService().update(this.charlaPAgenda);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (CharlaPAgendaException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (CharlaPAgendaRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean(AppConstants.MESSAGE_CONTROLLER_BEAN))
				.setSuccessMessage("- Valor Tipo Maestra " + msg, null, null);
		return AppConstants.CHARLA_P_AGENDA_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public String createCharlaPAgenda() {
		charlaPAgenda = new CharlaPAgenda();
		charlaPAgenda.setId(null);
		charlaPAgenda.setUsuarioCrea(getLoggedUserID());
		charlaPAgenda.setUsuarioModifica(getLoggedUserID());
		charlaPAgenda.setIdCharlaPProg(charlaPAgendaSearch.getIdCharlaPProg());
		charlaPAgenda.setEstado(charlaPAgendaSearch.getEstado());
		//charlaPAgenda.setEstado("ACTIVO");
		return AppConstants.CHARLA_P_AGENDA_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String updateCharlaPAgenda() {
		Map params = getExternalContext().getRequestParameterMap();
		String charlaPAgendaDataIndex = (String) params
				.get("charlaPAgendaDataIndex");
		this.charlaPAgenda = (CharlaPAgenda) ((List) this.charlaPAgendaListModel
				.getWrappedData())
				.get(Integer.parseInt(charlaPAgendaDataIndex));
		return AppConstants.CHARLA_P_AGENDA_UPDATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String cancelCharlaPAgenda() {
		return AppConstants.CHARLA_P_AGENDA_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public CharlaPAgenda getCharlaPAgendaSearch() {
		return charlaPAgendaSearch;
	}

	/**
	 * 
	 * @param charlaPAgendaSearch
	 */
	public void setcharlaPAgendaSearch(CharlaPAgenda charlaPAgendaSearch) {
		this.charlaPAgendaSearch = charlaPAgendaSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getcharlaPAgendaListModel() {
		return charlaPAgendaListModel;
	}

	/**
	 * 
	 * @param charlaPAgendaListModel
	 */
	public void setcharlaPAgendaListModel(DataModel charlaPAgendaListModel) {
		this.charlaPAgendaListModel = charlaPAgendaListModel;
	}

	/**
	 * 
	 * @return
	 */
	public CharlaPAgenda getCharlaPAgenda() {
		return charlaPAgenda;
	}

	/**
	 * 
	 * @param charlaPAgenda
	 */
	public void setcharlaPAgenda(CharlaPAgenda charlaPAgenda) {
		this.charlaPAgenda = charlaPAgenda;
	}

	/**
	 * obtencion del servicio charlaPAgendaService
	 * 
	 * @return
	 */
	public ICharlaPAgendaService getCharlaPAgendaService() {
		return charlaPAgendaService;
	}

	/**
	 * establecimiento del servicio charlaPAgendaService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param charlaPAgendaService
	 */
	public void setCharlaPAgendaService(
			ICharlaPAgendaService charlaPAgendaService) {
		this.charlaPAgendaService = charlaPAgendaService;
	}

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
